package cn.ekuma.epos.datalogic.businesslogic.orderprocess.impl;

import java.util.UUID;

import com.openbravo.bean.PaymentInfo;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.bean.shard.IncludeTaxs;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.sentence.SentenceExec;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.format.Formats;
import cn.ekuma.epos.bean.util.erp.OrderHelper;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.AbstractOrderProcess;

public class FundOrderProcess extends AbstractOrderProcess {

	@Override
	public void saveOrder(I_DataLogicERP dlERP, final Order order, I_Session s)
			throws BasicException {
	   OrderType oType = order.getDefineType();
        if (oType.getFundType() != OrderType.FUNDTYPE_NoChange) {
            SentenceExec paymentinsert = new PreparedSentence(s, "INSERT INTO PAYMENTS (ID, RECEIPT, PAYMENT, TOTAL, TRANSID, RETURNMSG) VALUES (?, ?, ?, ?, ?, ?)", SerializerWriteParams.INSTANCE);
            for (final PaymentInfo p : order.getPayments()) {
                paymentinsert.exec(new DataParams() {
                    public void writeValues() throws BasicException {
                        setString(1, UUID.randomUUID().toString());
                        setString(2, order.getId());
                        setString(3, p.getName());
                        double total = p.getTotal();
                        if ((order.getOrderType() == Order.ORDERBEARING_NORMAL && OrderType.FUNDTYPE_Out == order.getDefineType().getFundType()) || (order.getOrderType() == Order.ORDERBEARING_REVERSE && OrderType.FUNDTYPE_In == order.getDefineType().getFundType())) {
                            total = -total;
                        }
                        setDouble(4, total);
                        setString(5, OrderHelper.getTransactionID(order));
                        setBytes(6, (byte[]) Formats.BYTEA.parseValue(OrderHelper.getReturnMessage(order)));
                    }
                });

                if ("debt".equals(p.getName()) || "debtpaid".equals(p.getName())) {
                    double total = p.getTotal();
                    if ((order.getOrderType() == Order.ORDERBEARING_NORMAL && OrderType.FUNDTYPE_Out == order.getDefineType().getFundType()) || (order.getOrderType() == Order.ORDERBEARING_REVERSE && OrderType.FUNDTYPE_In == order.getDefineType().getFundType())) {
                        total = -total;
                    }
                    // udate customer fields...
                    dlERP.updateCurDebt(-p.getTotal(), order.getDate(), order.getCustomer().getId()); 
                     
                }
            }

            SentenceExec taxlinesinsert = new PreparedSentence(s, "INSERT INTO TAXLINES (ID, RECEIPT, TAXID, BASE, AMOUNT)  VALUES (?, ?, ?, ?, ?)", SerializerWriteParams.INSTANCE);
            if (order.getTaxes() != null) {
                for (final IncludeTaxs tickettax : order.getTaxes()) {
                    taxlinesinsert.exec(new DataParams() {
                        public void writeValues() throws BasicException {
                            setString(1, UUID.randomUUID().toString());
                            setString(2, order.getId());
                            setString(3, tickettax.getTaxInfo().getId());
                            setDouble(4, tickettax.getSubTotal());
                            setDouble(5, tickettax.getTax());
                        }
                    });
                }
            }
        }
	}
	

	@Override
	public void deleteOrder(I_DataLogicERP dlERP, Order order, I_Session s)
			throws BasicException {
		OrderType oType = order.getDefineType();
        if (oType.getFundType() != OrderType.FUNDTYPE_NoChange) {
        	for (final PaymentInfo p : order.getPayments()) {
                if ("debt".equals(p.getName()) || "debtpaid".equals(p.getName())) {
                    double total = p.getTotal();
                    if ((order.getOrderType() == Order.ORDERBEARING_NORMAL && OrderType.FUNDTYPE_In == order.getDefineType().getFundType()) || (order.getOrderType() == Order.ORDERBEARING_REVERSE && OrderType.FUNDTYPE_Out == order.getDefineType().getFundType())) {
                        total = -total;
                    }
                    // udate customer fields...
                    dlERP.updateCurDebt(p.getTotal(), order.getDate(), order.getCustomer().getId()); 
                }
            }
        	
        	new StaticSentence(s, "DELETE FROM PAYMENTS WHERE RECEIPT = ?", SerializerWriteString.INSTANCE).exec(order.getId());
        }
	}

	@Override
	public String getName() {
		return "FundOrderProcess";
	}

}
